﻿using Learun.Util.SqlSugar;
using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    public class ec_notificationService
    {
        #region 仓储
        Repository<ec_projectEntity> _projectRepository => new Repository<ec_projectEntity>();
        Repository<ec_notificationEntity> _notificationRepository => new Repository<ec_notificationEntity>();
        #endregion
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_notificationEntity entity, string ProjectId)
        {
            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _notificationRepository.Update(entity);
                    }
                    else
                    {
                        var ProjectEntity = _projectRepository.GetById(ProjectId);
                        //this.BaseRepository().UpdateByNoMap(entity, $"ec_notification_{ProjectEntity.ProjectIndex}", "ID");
                        _notificationRepository.AsUpdateable(entity).AS($"ec_notification_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
                else
                {
                    entity.Create();
                    if (string.IsNullOrWhiteSpace(ProjectId))
                    {
                        _notificationRepository.Insert(entity);
                    }
                    else
                    {
                        var ProjectEntity = _projectRepository.GetById(ProjectId);
                        //this.BaseRepository().InsertByNoMap(entity, $"ec_notification_{ProjectEntity.ProjectIndex}");
                        _notificationRepository.AsInsertable(entity)
                            .IgnoreColumns(x => x.CheckTime)
                            .IgnoreColumns(x => x.UpdateTime)
                            .IgnoreColumns(x => x.UpdateUserID)
                            .AS($"ec_notification_{ProjectEntity.ProjectIndex}").ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }


        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_notificationEntity> GetList(string queryJson)
        {
            try
            {
                //参考写法
                var queryParam = queryJson.ToJObject();
                var strSql = new StringBuilder();
                strSql.Append(@"SELECT t1.*");
                if (queryParam["ProjectId"].IsEmpty())
                {
                    strSql.Append(" FROM ec_notification t1 ");
                }
                else
                {
                    //var ProjectEntity = this.BaseRepository().FindEntity<ec_projectEntity>(queryParam["ProjectId"].ToString());
                    var ProjectEntity = _projectRepository.GetById(queryParam["ProjectId"].ToString());
                    strSql.Append($"  FROM ec_notification_{ProjectEntity.ProjectIndex} t1 ");
                }
                strSql.Append(" where 1=1 ");
                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                var dp = new List<SugarParameter>();
                if (!queryParam["RetrieveUserID"].IsEmpty())
                {
                    dp.Add(new SugarParameter("RetrieveUserID", queryParam["RetrieveUserID"].ToString(), DbType.String));
                    strSql.Append(" AND t1.RetrieveUserID = @RetrieveUserID ");
                }
                //return this.BaseRepository().FindList<ec_notificationEntity>(strSql.ToString(), dp);
                return SqlSugarHelper.Db.SqlQueryable<ec_notificationEntity>(strSql.ToString()).AddParameters(dp).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
    }
}
